package com.demo.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.logging.MemoryHandler;

/**
 * 元数据对象处理器，负责自动填充数据
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    //新增时自动填充数据
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("新增时自动填充数据");
        log.info(metaObject.toString());


        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }

    //修改时自动填充数据
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("修改时自动填充数据");
        log.info(metaObject.toString());

        long id = Thread.currentThread().getId();
        log.info("当前线程id为 {}" ,id);
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }
}
